from zope.interface import implements, Interface

from Products.Five import BrowserView
from Products.CMFCore.utils import getToolByName
from zope.component import getUtility
from zope.i18n.interfaces import ITranslationDomain
from zope.component import queryUtility
from Products.CMFCore.interfaces import ISiteRoot

from my315ok.fangcan import fangcanMessageFactory as _
from my315ok.fangcan.interfaces import Ihouse

category_map = {"new house":"0","secondhand house":"1","rent house":"2","rend house":"2"}

class ImigrateView(Interface):
    """
    migrate view interface
    """

    def test():
        """ test method"""


class migrateView(BrowserView):
    """
    migrate browser view
    """
    implements(ImigrateView)

    def __init__(self, context, request):
        self.context = context
        self.request = request

    @property
    def portal_catalog(self):
        return getToolByName(self.context, 'portal_catalog')

    @property
    def portal(self):
        return getToolByName(self.context, 'portal_url').getPortalObject()

    def migrate_category(self):
        """
        test method
        """
        portal = getUtility(ISiteRoot)
        pc = getToolByName(portal, 'portal_catalog')
        brains = pc(object_provides=Ihouse.__identifier__)
        for bn in brains:
            key = bn.category
            obj = bn.getObject()
            if key in  category_map.keys():
                value = category_map[key]
                obj.setCategory(value)
                obj.reindexObject(idxs=['category'])

        return "%s recorders have been updated!" % len(brains)

    def migrate_Bigarea(self):
        """
        test method
        """
        portal = getUtility(ISiteRoot)
        pc = getToolByName(portal, 'portal_catalog')
        brains = pc(object_provides=Ihouse.__identifier__)
        for bn in brains:
            key = bn.Bigarea
            obj = bn.getObject()
            if len(key) > 0:
                obj.setBigarea('xiangtanshi')
#                obj.setArea(key)
                obj.reindexObject(idxs=['Bigarea'])

        return "%s recorders have been updated!" % len(brains)
    def migrate_clearimag(self):
        """
        test method
        """
        portal = getUtility(ISiteRoot)
        pc = getToolByName(portal, 'portal_catalog')
        brains = pc(object_provides=Ihouse.__identifier__)
        for bn in brains:           
            obj = bn.getObject()
            watermark = ""
            obj.setImage(watermark,content_type="image/jpg",filename='fcwatermaik.jpg')                     
        return "%s recorders have been updated!" % len(brains)
#house.setImage(watermark,content_type="image/jpg",filename='fcwatermaik.jpg')
    def migrate_area(self):
        """
        test method
        """
        portal = getUtility(ISiteRoot)
        voc = {"雨湖区":"yuhu","岳塘区":"yuetang","九华":"jiuhua","易俗河":"yisuhe"}
        pc = getToolByName(portal, 'portal_catalog')
#        import pdb
#        pdb.set_trace()
        brains = pc(object_provides=Ihouse.__identifier__)
        for bn in brains:
            key = bn.Area
            for k in voc.keys():
                if k.encode('utf-8') == key:                               
                    obj = bn.getObject()
                    obj.setArea(voc[k])            
                    obj.reindexObject(idxs=['Area'])

        return "%s recorders have been updated!" % len(brains)
    
    def migrate_title(self):
        """
        test method
        """
        portal = getUtility(ISiteRoot)
        voc = {"0":"热卖","1":"出售","2":"出租"}
        pc = getToolByName(portal, 'portal_catalog')
#        import pdb
#        pdb.set_trace()
        brains = pc(object_provides=Ihouse.__identifier__)
        for bn in brains:
            key = bn.category                
            if key in voc.keys():                                               
                obj = bn.getObject()
                obj.setTitle(voc[key])            
                obj.reindexObject(idxs=['Title'])

        return "%s recorders have been updated!" % len(brains)